package main

import (
	"context"
	"fmt"
	"github.com/apache/pulsar-client-go/pulsar"
	"github.com/golang-jwt/jwt"
	"log"
	"time"
)

func main() {
	// Pulsar服务URL
	serviceURL := "pulsar://{your ip}:{your port}"
	authToken := "{your token}"

	// 创建Pulsar客户端
	client, err := pulsar.NewClient(pulsar.ClientOptions{
		URL:            serviceURL,
		Authentication: pulsar.NewAuthenticationToken(authToken),
	})
	if err != nil {
		log.Fatal(err)
	}

	defer client.Close()

	// 定义租户和命名空间
	tenant := "{your tenant}"
	namespace := "{your namespace}"
	topic := fmt.Sprintf("persistent://%s/%s/topic_promotion_of_cultural_activities", tenant, namespace)

	u := new(uint)
	// 创建生产者
	producer, err := client.CreateProducer(pulsar.ProducerOptions{
		Topic:                topic,
		CompressionType:      pulsar.NoCompression,
		MaxPendingMessages:   1,
		MaxReconnectToBroker: u,
	})
	if err != nil {
		log.Fatal(err)
	}

	defer producer.Close()

	// 发送消息
	ctx := context.Background()
	msgContent := "{your data}"
	msgID, err := producer.Send(ctx, &pulsar.ProducerMessage{
		Payload: []byte(msgContent),
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("Message published: %s, ID: %v\n", msgContent, msgID)
}
func createToken(username, password string) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
		"sub": username,
		"exp": time.Now().Add(time.Hour * 24).Unix(),
	})

	tokenString, err := token.SignedString([]byte(password))
	if err != nil {
		return "", err
	}
	return tokenString, nil
}
